package com.rskj.cashbook.vo;

import lombok.Data;
import lombok.EqualsAndHashCode;
import lombok.experimental.Accessors;

import java.math.BigDecimal;
import java.time.LocalDate;
import java.util.List;

/**
 * 自动记账规则模板实体类
 * @author system
 */
@Data
@EqualsAndHashCode(callSuper = true)
@Accessors(chain = true)
public class AutoAccountingRuleTemplate extends BaseEntity {
    
    /**
     * 模板名称
     */
    private String templateName;
    
    /**
     * 模板描述
     */
    private String description;
    
    /**
     * 模板分类：COMMON(通用)、INCOME(收入)、EXPENSE(支出)、TRANSFER(转账)
     */
    private String category;
    
    /**
     * 模板标签
     */
    private List<String> tags;
    
    /**
     * 默认金额
     */
    private BigDecimal defaultAmount;
    
    /**
     * 默认支付人
     */
    private String defaultPayUserName;
    
    /**
     * 默认受益人
     */
    private String defaultForUserName;
    
    /**
     * 默认支付方式
     */
    private String defaultPayWay;
    
    /**
     * 默认交易对方
     */
    private String defaultPayPlat;
    
    /**
     * 默认备注
     */
    private String defaultRemark;
    
    /**
     * 默认执行频率
     */
    private String defaultFrequency;
    
    /**
     * 默认频率值
     */
    private Integer defaultFrequencyValue;
    
    /**
     * 默认开始日期
     */
    private LocalDate defaultStartDate;
    
    /**
     * 默认结束日期
     */
    private LocalDate defaultEndDate;
    
    /**
     * 默认工作日限制
     */
    private String defaultWeekdays;
    
    /**
     * 默认月份日期限制
     */
    private String defaultMonthDays;
    
    /**
     * 是否启用
     */
    private Boolean isActive;
    
    /**
     * 使用次数
     */
    private Integer usageCount;
    
    /**
     * 模板版本
     */
    private String version;
    
    /**
     * 模板作者
     */
    private String author;
    
    /**
     * 适用场景描述
     */
    public String getCategoryDescription() {
        if (category == null) return "";
        
        switch (category) {
            case "COMMON":
                return "通用";
            case "INCOME":
                return "收入";
            case "EXPENSE":
                return "支出";
            case "TRANSFER":
                return "转账";
            default:
                return category;
        }
    }
    
    /**
     * 获取模板使用说明
     */
    public String getUsageInstructions() {
        StringBuilder instructions = new StringBuilder();
        
        if (defaultAmount != null) {
            instructions.append("默认金额: ").append(defaultAmount).append("元\n");
        }
        
        if (defaultFrequency != null) {
            instructions.append("执行频率: ").append(getFrequencyDescription()).append("\n");
        }
        
        if (defaultPayUserName != null) {
            instructions.append("支付人: ").append(defaultPayUserName).append("\n");
        }
        
        if (defaultForUserName != null) {
            instructions.append("受益人: ").append(defaultForUserName).append("\n");
        }
        
        if (defaultRemark != null) {
            instructions.append("备注: ").append(defaultRemark);
        }
        
        return instructions.toString();
    }
    
    /**
     * 频率描述
     */
    private String getFrequencyDescription() {
        if (defaultFrequency == null) return "";
        
        switch (defaultFrequency) {
            case "DAILY":
                return "每天";
            case "WEEKLY":
                return "每周" + (defaultFrequencyValue != null ? getWeekdayName(defaultFrequencyValue) : "");
            case "MONTHLY":
                return "每月" + (defaultFrequencyValue != null ? defaultFrequencyValue + "号" : "");
            case "YEARLY":
                return "每年";
            default:
                return defaultFrequency;
        }
    }
    
    private String getWeekdayName(int weekday) {
        String[] weekdays = {"", "周一", "周二", "周三", "周四", "周五", "周六", "周日"};
        return weekday >= 1 && weekday <= 7 ? weekdays[weekday] : "";
    }
}
